﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using RBS.DBUtility;
using RBS.Common;
namespace RBS.Core.DAL
    
{
    //数据字典
    public partial class SYS_DictInfo
    {

        public bool Exists(int DictID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SYS_DictInfo");
            strSql.Append(" where ");
            strSql.Append(" DictID = @DictID  ");
            SqlParameter[] parameters = {
					new SqlParameter("@DictID", SqlDbType.Int,4)			};
            parameters[0].Value = DictID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RBS.Core.Model.SYS_DictInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SYS_DictInfo(");
            strSql.Append("DictName,DictCode,DictType,ParentID,Path,Sequence,SQLCMD,Remark,LastUpdateUserName,LastUpdateUserFullName,LastUpdateTime,CreateUserName,CreateUserFullName,CreateDateTime)");
            strSql.Append(" values (");
            strSql.Append("@DictName,@DictCode,@DictType,@ParentID,@Path,@Sequence,@SQLCMD,@Remark,@LastUpdateUserName,@LastUpdateUserFullName,@LastUpdateTime,@CreateUserName,@CreateUserFullName,@CreateDateTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@DictName", SqlDbType.VarChar,100),
					new SqlParameter("@DictCode", SqlDbType.VarChar,50),
					new SqlParameter("@DictType", SqlDbType.TinyInt,1),
					new SqlParameter("@ParentID", SqlDbType.Int,4),
					new SqlParameter("@Path", SqlDbType.VarChar,1000),
					new SqlParameter("@Sequence", SqlDbType.Int,4),
					new SqlParameter("@SQLCMD", SqlDbType.VarChar,4000),
					new SqlParameter("@Remark", SqlDbType.VarChar,200),
					new SqlParameter("@LastUpdateUserName", SqlDbType.VarChar,50),
					new SqlParameter("@LastUpdateUserFullName", SqlDbType.VarChar,50),
					new SqlParameter("@LastUpdateTime", SqlDbType.DateTime),
					new SqlParameter("@CreateUserName", SqlDbType.VarChar,50),
					new SqlParameter("@CreateUserFullName", SqlDbType.VarChar,50),
					new SqlParameter("@CreateDateTime", SqlDbType.DateTime)};
            parameters[0].Value = model.DictName;
            parameters[1].Value = model.DictCode;
            parameters[2].Value = model.DictType;
            parameters[3].Value = model.ParentID;
            parameters[4].Value = model.Path;
            parameters[5].Value = model.Sequence;
            parameters[6].Value = model.SQLCMD;
            parameters[7].Value = model.Remark;
            parameters[8].Value = model.LastUpdateUserName;
            parameters[9].Value = model.LastUpdateUserFullName;
            parameters[10].Value = model.LastUpdateTime;
            parameters[11].Value = model.CreateUserName;
            parameters[12].Value = model.CreateUserFullName;
            parameters[13].Value = model.CreateDateTime;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
          

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RBS.Core.Model.SYS_DictInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SYS_DictInfo set ");

        
            strSql.Append(" LastUpdateUserName = @LastUpdateUserName , ");
            strSql.Append(" LastUpdateUserFullName = @LastUpdateUserFullName , ");
            strSql.Append(" LastUpdateTime = @LastUpdateTime , ");
            strSql.Append(" CreateUserName = @CreateUserName , ");
            strSql.Append(" CreateUserFullName = @CreateUserFullName , ");
            strSql.Append(" CreateDateTime = @CreateDateTime , ");
            strSql.Append(" DictName = @DictName , ");
            strSql.Append(" DictCode = @DictCode , ");
            strSql.Append(" DictType = @DictType , ");
            strSql.Append(" ParentID = @ParentID , ");
            strSql.Append(" Path = @Path , ");
            strSql.Append(" Sequence = @Sequence , ");
            strSql.Append(" SQLCMD = @SQLCMD , ");
            strSql.Append(" Remark = @Remark  ");
            strSql.Append(" where DictID=@DictID  ");

            SqlParameter[] parameters = {
			            new SqlParameter("@DictID", SqlDbType.Int,4) ,            
                        new SqlParameter("@LastUpdateUserName", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@LastUpdateUserFullName", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@LastUpdateTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@CreateUserName", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@CreateUserFullName", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@CreateDateTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@DictName", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@DictCode", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@DictType", SqlDbType.TinyInt,1) ,            
                        new SqlParameter("@ParentID", SqlDbType.Int,4) ,            
                        new SqlParameter("@Path", SqlDbType.VarChar,1000) ,            
                        new SqlParameter("@Sequence", SqlDbType.Int,4) ,            
                        new SqlParameter("@SQLCMD", SqlDbType.VarChar,4000) ,            
                        new SqlParameter("@Remark", SqlDbType.VarChar,200)             
              
            };

            parameters[0].Value = model.DictID;
            parameters[1].Value = model.LastUpdateUserName;
            parameters[2].Value = model.LastUpdateUserFullName;
            parameters[3].Value = model.LastUpdateTime;
            parameters[4].Value = model.CreateUserName;
            parameters[5].Value = model.CreateUserFullName;
            parameters[6].Value = model.CreateDateTime;
            parameters[7].Value = model.DictName;
            parameters[8].Value = model.DictCode;
            parameters[9].Value = model.DictType;
            parameters[10].Value = model.ParentID;
            parameters[11].Value = model.Path;
            parameters[12].Value = model.Sequence;
            parameters[13].Value = model.SQLCMD;
            parameters[14].Value = model.Remark;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int DictID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SYS_DictInfo ");
            strSql.Append(" where DictID=@DictID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DictID", SqlDbType.Int,4)			};
            parameters[0].Value = DictID;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RBS.Core.Model.SYS_DictInfo GetModel(int DictID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DictID, LastUpdateUserName, LastUpdateUserFullName, LastUpdateTime, CreateUserName, CreateUserFullName, CreateDateTime, DictName, DictCode, DictType, ParentID, Path, Sequence, SQLCMD, Remark  ");
            strSql.Append("  from SYS_DictInfo ");
            strSql.Append(" where DictID=@DictID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DictID", SqlDbType.Int,4)			};
            parameters[0].Value = DictID;


            RBS.Core.Model.SYS_DictInfo model = new RBS.Core.Model.SYS_DictInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                model.DictID = int.Parse(ds.Tables[0].Rows[0]["DictID"].ToString());
                model.LastUpdateUserName = ds.Tables[0].Rows[0]["LastUpdateUserName"].ToString();
                model.LastUpdateUserFullName = ds.Tables[0].Rows[0]["LastUpdateUserFullName"].ToString();
                if (ds.Tables[0].Rows[0]["LastUpdateTime"].ToString() != "")
                {
                    model.LastUpdateTime = DateTime.Parse(ds.Tables[0].Rows[0]["LastUpdateTime"].ToString());
                }
                model.CreateUserName = ds.Tables[0].Rows[0]["CreateUserName"].ToString();
                model.CreateUserFullName = ds.Tables[0].Rows[0]["CreateUserFullName"].ToString();
                if (ds.Tables[0].Rows[0]["CreateDateTime"].ToString() != "")
                {
                    model.CreateDateTime = DateTime.Parse(ds.Tables[0].Rows[0]["CreateDateTime"].ToString());
                }
                model.DictName = ds.Tables[0].Rows[0]["DictName"].ToString();
                model.DictCode = ds.Tables[0].Rows[0]["DictCode"].ToString();
                if (ds.Tables[0].Rows[0]["DictType"].ToString() != "")
                {
                    model.DictType = int.Parse(ds.Tables[0].Rows[0]["DictType"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParentID"].ToString() != "")
                {
                    model.ParentID = int.Parse(ds.Tables[0].Rows[0]["ParentID"].ToString());
                }
                model.Path = ds.Tables[0].Rows[0]["Path"].ToString();
                if (ds.Tables[0].Rows[0]["Sequence"].ToString() != "")
                {
                    model.Sequence = int.Parse(ds.Tables[0].Rows[0]["Sequence"].ToString());
                }
                model.SQLCMD = ds.Tables[0].Rows[0]["SQLCMD"].ToString();
                model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM SYS_DictInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM SYS_DictInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        #region  ExtensionMethod
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM SYS_DictInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where 1=1 " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
        #endregion  ExtensionMethod



       /// <summary>
       /// 删除当前数据字典和子集数据字典
       /// </summary>
       /// <param name="dictId">当前数据字典ID</param>
       /// <returns></returns>
        public bool DeleteChild(int dictId)
        { 
            string strSql = "delete from SYS_DictInfo where DictID in (select DictID from [GetDictInfoChild](@dictId))";
            SqlParameter[] parameters = {
					new SqlParameter("@dictId", SqlDbType.Int,4)	 };
            parameters[0].Value = dictId; 
            int rows = DbHelperSQL.ExecuteSql(strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            } 
        }
    }
}

